<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme
import resize from "./mixins/resize";
import { getHomeZs } from "@/api/system/shops";
export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "300px",
    },
  },
  data() {
    return {
      chart: null,
      home: [
        { value: 320, name: "空置" },
        { value: 240, name: "出租" },
        { value: 149, name: "出售" },
      ],
    };
  },
  mounted() {
    this.getHome();
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    //获取租售数据
    getHome() {
      getHomeZs().then((resp) => {
        for (let i = 0; i < resp.data.length; i++) {
          this.home[i].value = resp.data[i];
        }
        this.$nextTick(() => {
          this.initChart();
        });
      });
    },
    initChart() {
      this.chart = echarts.init(this.$el, "macarons");

      this.chart.setOption({
        tooltip: {
          trigger: "item",
          formatter: "{a} <br/>{b} : {c} ({d}%)",
        },
        legend: {
          left: "center",
          bottom: "10",
          data: ["空置", "出租", "出售"],
        },
        series: [
          {
            name: "商铺租售情况",
            type: "pie",
            roseType: "radius",
            radius: [15, 95],
            center: ["50%", "38%"],
            data: this.home,
            animationEasing: "cubicInOut",
            animationDuration: 2600,
          },
        ],
      });
    },
  },
};
</script>
